﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.BLL;
using Com.CMS263.PO;
using Com.CMS263.Util;
using System.IO;

namespace Com.CMS263.Web.manage.ProdAttr
{
    public partial class ProdTypeAtt : System.Web.UI.Page
    {
        public int page = 1;
        int id = 0;
        ProdTypeBLL ptbll = new ProdTypeBLL();
        public ProdType pt = new ProdType();
        ProdAttributeBLL pabll = new ProdAttributeBLL();
        public IList<ProdAttribute> plist = new List<ProdAttribute>();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                id = int.Parse(Request["ptid"]);
                page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
                this.lbTypeName.Text = "商品类型扩展";
                //根据父类类型盘点是否出默认结果               
                if (id != 0)
                {
                    pt = (ProdType)ptbll.FindModelById("ProdType", id);
                    lblTypeName.Text = pt.TypeName;
                    hfptid.Value = pt.Id.ToString();
                }
                else
                {
                    Response.Redirect("PAList.aspx?page="+page);
                }
            }
            plist = pabll.GetAllByType(id);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int ptid = Convert.ToInt32(Request["ptid"]);
            if (ptid <= 0)
            {
                StringUtil.Alert("请选择要编辑扩展属性的类型", "palist.aspx");
            }
            else
            {
                int id = Convert.ToInt32(hfId.Value);
                ProdAttribute pab = new ProdAttribute();
                string attrname = StringUtil.checkStr(txtAttrName.Text);
                string attrenname = StringUtil.checkStr(txtAttrEnName.Text);
                string attrvalue = StringUtil.checkStr(Request["txtAttrValue"]);
                if (string.IsNullOrEmpty(attrvalue))
                {
                    attrvalue = "";
                }

                string defaultvalue = StringUtil.checkStr(txtDefaultValue.Text);
                if (Convert.ToInt32(Request["ddlType"]) == 0)
                {
                    StringUtil.Alert("请选择属性类型", "-1");
                }
                if (id <= 0)
                {
                    pab.ProdTypeId = ptid;
                    pab.AttrEnName = attrenname;
                    pab.AttrName = attrname;
                    pab.AttrType = Convert.ToInt32(Request["ddlType"]);
                    pab.AttrValue = attrvalue;
                    pab.DefaultValue = defaultvalue;
                    pab.IsBackShow = chbShowBack.Checked;
                    pab.IsNull = chbIsNull.Checked;
                    pabll.entity = pab;
                    pabll.Add();
                    StringUtil.Alert("添加成功", "ProdTypeAtt.aspx?ptid=" + ptid);
                }
                else
                {
                    StringUtil.Alert("修改成功", "-1");
                }
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            int page = Convert.ToInt32(Request["page"]);
            Response.Redirect("palist.aspx?page=" + page);
        }
        public string GetTypeValue(int typeid)
        {
            if (typeid == 1)
            {
                return "单行文本框";
            }
            if (typeid == 2)
            {
                return "多行文本框";
            }
            if (typeid == 3)
            {
                return "在线编辑器";
            }
            if (typeid == 4)
            {
                return "上传图片";
            }
            if (typeid == 5)
            {
                return "上传文件";
            }
            if (typeid == 6)
            {
                return "单选框";
            }
            if (typeid == 7)
            {
                return "复选框";
            }
            if (typeid == 8)
            {
                return "下拉框";
            }
            else
            {
                return "未知";
            }
        }

      
    }
}
